package etxt2db.main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import org.apache.tools.ant.taskdefs.Copy;
import org.mitre.jawb.io.SgmlDocument;

public class TrainTestGeneration {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		String FolderName = "/home/pjbarrio/Dataset/NaturalDisaster/SimpleData/";
		
		String dataFolderName = FolderName + "";
		
		int numberOfSets = 10;
		
		double split = 0.75;

		File dataFolder = new File(dataFolderName);
		
		File[] files = dataFolder.listFiles();
		
		
		
		Vector<File> auxFiles = new Vector<File>();
		
		for (int i = 0; i < files.length; i++) {
			
			auxFiles.add(files[i]);
			
		}
		
		for (int i = 0; i < numberOfSets; i++) {
			
			
			File train = new File(FolderName + i + "/trainingDirectory");
			
			File taggedTest = new File(FolderName + i + "/taggedtestingDirectory");
			
			File test = new File(FolderName + i + "/testingDirectory");
			
			train.mkdirs();
			
			taggedTest.mkdirs();
		
			test.mkdirs();
			
			Collections.shuffle(auxFiles,new Random());
			
			int j;
						
			for ( j = 0; j < files.length*split; j++) {
				
				copyFiles(auxFiles.get(j), new File(train.getAbsolutePath() + "/" + auxFiles.get(j).getName()));
			}
			
			for (; j < files.length; j++) {
			
				copyFiles(auxFiles.get(j), new File(taggedTest.getAbsolutePath() + "/" + auxFiles.get(j).getName()));
				
				cleanTags(auxFiles.get(j), new File(test.getAbsolutePath() + "/" + auxFiles.get(j).getName()));
				
			}
			
		}
		
	}

	private static void cleanTags(File file, File destiny) throws FileNotFoundException, IOException {
		
		SgmlDocument sgml = new SgmlDocument(new FileReader(file));
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(destiny));
		
		bw.write(sgml.getSignalText());
		
		bw.close();
		
	}

	private static void copyFiles(File from, File to) {

		Copy copier = new Copy();
				
		copier.setFile(from);
		
		copier.setTofile(to);
		
		copier.execute();

		
	}

}
